package com.fyzk.activity.lottery.jc.beidan;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.lottery.jc.football.newJz.data.QQListView;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by fanxs11 on 16/4/26.
 */
public class BeiDan_ZJQ_Adapter extends BeiDanAdapter{

    public BeiDan_ZJQ_Adapter(Context context, List<BeiDanBean> infos, QQListView listView, OnBeiDanCheckBallListener listener) {
        super(context, infos, listView, listener);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ChildViewHolder childViewHolder;
        final BeiDanChildBean info = infos.get(groupPosition).childBeen.get(childPosition);
//
        if (convertView == null) {
            childViewHolder = new ChildViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.beidan_list_child_zjq, null);
            childViewHolder.matchNum_tv = convertView.findViewById(R.id.beidan_list_matchNum_tv);
            childViewHolder.matchName_tv = convertView.findViewById(R.id.beidan_list_matchName_tv);
            childViewHolder.endSaleTime_tv = convertView.findViewById(R.id.beidan_list_endSaleTime_tv);
            childViewHolder.home_tv = convertView.findViewById(R.id.beidan_list_home);
            childViewHolder.away_tv = convertView.findViewById(R.id.beidan_list_away);
            childViewHolder.zjq_0 = convertView.findViewById(R.id.beidan_list_zjq_0);
            childViewHolder.zjq_1 = convertView.findViewById(R.id.beidan_list_zjq_1);
            childViewHolder.zjq_2 = convertView.findViewById(R.id.beidan_list_zjq_2);
            childViewHolder.zjq_3 = convertView.findViewById(R.id.beidan_list_zjq_3);
            childViewHolder.zjq_4 = convertView.findViewById(R.id.beidan_list_zjq_4);
            childViewHolder.zjq_5 = convertView.findViewById(R.id.beidan_list_zjq_5);
            childViewHolder.zjq_6 = convertView.findViewById(R.id.beidan_list_zjq_6);
            childViewHolder.zjq_7 = convertView.findViewById(R.id.beidan_list_zjq_7);

            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        childViewHolder.matchNum_tv.setText(info.getMatchNum()+"");
        childViewHolder.matchName_tv.setText(info.getMatchName());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        String endTimeStr = simpleDateFormat.format(new Date(info.getEndSaleTime()));
        childViewHolder.endSaleTime_tv.setText(endTimeStr+"截止");
        childViewHolder.home_tv.setText(info.getHomeTeam());
        childViewHolder.away_tv.setText(info.getAwayTeam());

        childViewHolder.zjq_0.setText("0\n"+info.getScore_0());
        childViewHolder.zjq_1.setText("1\n"+info.getScore_1());
        childViewHolder.zjq_2.setText("2\n"+info.getScore_2());
        childViewHolder.zjq_3.setText("3\n"+info.getScore_3());
        childViewHolder.zjq_4.setText("4\n"+info.getScore_4());
        childViewHolder.zjq_5.setText("5\n"+info.getScore_5());
        childViewHolder.zjq_6.setText("6\n"+info.getScore_6());
        childViewHolder.zjq_7.setText("7+\n"+info.getScore_7());

        childViewHolder.zjq_0.setChecked(info.isScore_0);
        childViewHolder.zjq_1.setChecked(info.isScore_1);
        childViewHolder.zjq_2.setChecked(info.isScore_2);
        childViewHolder.zjq_3.setChecked(info.isScore_3);
        childViewHolder.zjq_4.setChecked(info.isScore_4);
        childViewHolder.zjq_5.setChecked(info.isScore_5);
        childViewHolder.zjq_6.setChecked(info.isScore_6);
        childViewHolder.zjq_7.setChecked(info.isScore_7);

        info.btnHGPojo.get(info.SCORE_0).isSel = info.isScore_0;
        info.btnHGPojo.get(info.SCORE_1).isSel = info.isScore_1;
        info.btnHGPojo.get(info.SCORE_2).isSel = info.isScore_2;
        info.btnHGPojo.get(info.SCORE_3).isSel = info.isScore_3;
        info.btnHGPojo.get(info.SCORE_4).isSel = info.isScore_4;
        info.btnHGPojo.get(info.SCORE_5).isSel = info.isScore_5;
        info.btnHGPojo.get(info.SCORE_6).isSel = info.isScore_6;
        info.btnHGPojo.get(info.SCORE_7).isSel = info.isScore_7;
        info.btnHGPojo.put(info.SCORE_0,info.btnHGPojo.get(info.SCORE_0));
        info.btnHGPojo.put(info.SCORE_1,info.btnHGPojo.get(info.SCORE_1));
        info.btnHGPojo.put(info.SCORE_2,info.btnHGPojo.get(info.SCORE_2));
        info.btnHGPojo.put(info.SCORE_3,info.btnHGPojo.get(info.SCORE_3));
        info.btnHGPojo.put(info.SCORE_4,info.btnHGPojo.get(info.SCORE_4));
        info.btnHGPojo.put(info.SCORE_5,info.btnHGPojo.get(info.SCORE_5));
        info.btnHGPojo.put(info.SCORE_6,info.btnHGPojo.get(info.SCORE_6));
        info.btnHGPojo.put(info.SCORE_7,info.btnHGPojo.get(info.SCORE_7));
        jiSuanChangCi();

        childViewHolder.zjq_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isScore_0 = !info.isScore_0;
                if (info.isScore_0) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.zjq_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isScore_1 = !info.isScore_1;
                if (info.isScore_1) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.zjq_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isScore_2 = !info.isScore_2;
                if (info.isScore_2) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.zjq_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isScore_3 = !info.isScore_3;
                if (info.isScore_3) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.zjq_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isScore_4 = !info.isScore_4;
                if (info.isScore_4) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.zjq_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isScore_5 = !info.isScore_5;
                if (info.isScore_5) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.zjq_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isScore_6 = !info.isScore_6;
                if (info.isScore_6) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        childViewHolder.zjq_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isScore_7 = !info.isScore_7;
                if (info.isScore_7) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });
        return convertView;
    }

    @Override
    protected void jiSuanChangCi() {
        changCi = 0;
        for (int i = 0; i < infos.size(); i++) {
            List<BeiDanChildBean> childBeens = infos.get(i).childBeen;
            for (int j = 0; j < childBeens.size(); j++) {
                if (childBeens.get(j).checkNum > 0) {
                    changCi++;
                }
            }
        }
        if (listener != null) {
            listener.onCheckBall(changCi);
        }
    }

    private class ChildViewHolder {
        TextView matchNum_tv;
        TextView matchName_tv;
        TextView endSaleTime_tv;
        TextView home_tv;
        TextView away_tv;
        CheckBox zjq_0;
        CheckBox zjq_1;
        CheckBox zjq_2;
        CheckBox zjq_3;
        CheckBox zjq_4;
        CheckBox zjq_5;
        CheckBox zjq_6;
        CheckBox zjq_7;
    }

}
